package com.example.fengyungo_user.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengyungo_user.R;
import com.example.fengyungo_user.activity.main.RouteActivity;
import com.example.fengyungo_user.constant.MapConstant;
import com.example.fengyungo_user.domain.Location;

import java.util.List;

public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.ViewHolder> {

    private static final String TAG = "LocationAdapter";

    protected Context mContext;

    private List<Location> locations;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView locationText;
        TextView distanceText;
        View positionView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            locationText = itemView.findViewById(R.id.tv_end_result);
            distanceText = itemView.findViewById(R.id.tv_distance);
            positionView = itemView;
        }
    }

    public LocationAdapter(List<Location> locations) {
        this.locations = locations;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_location_result, parent, false);

        ViewHolder holder = new ViewHolder(view);
        holder.positionView.setOnClickListener((v) -> {
            int position = holder.getLayoutPosition();
            Log.d(TAG, "onCreateViewHolder: position=" + position);
            Location location = locations.get(position);
            // 跳转到路线页面
            Intent intent = new Intent(mContext, RouteActivity.class);
            // 放入起点信息
            intent.putExtra(MapConstant.BEGIN_LAT, location.getBeginLatLonPoint().getLatitude());
            intent.putExtra(MapConstant.BEGIN_LON, location.getBeginLatLonPoint().getLongitude());
            intent.putExtra(MapConstant.BEGIN_ADDRESS, location.getBeginPosition());
            // 终点信息
            intent.putExtra(MapConstant.END_LAT, location.getEndLatLonPoint().getLatitude());
            intent.putExtra(MapConstant.END_LON, location.getEndLatLonPoint().getLongitude());
            intent.putExtra(MapConstant.END_ADDRESS, location.getEndPosition());
            // 距离
            intent.putExtra(MapConstant.DISTANCE, location.getDistance());
            mContext.startActivity(intent);
            Log.d(TAG, "onCreateViewHolder: location=" + location);
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Location location = locations.get(position);
        holder.locationText.setText(location.getEndPosition());
        holder.distanceText.setText(location.getDistance());
    }

    @Override
    public int getItemCount() {
        return locations.size();
    }
}
